﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace AppFabricAdministrator
{
    public partial class NewCache : UserControl
    {
        private string eviction = "LRU";
        private int ttl = 10;
        private int secondaries = 0;
        private bool expirable = true;
        private bool notificationEnabled = true;

        private static string NewCacheCmd = @"New-Cache -CacheName {0} -Eviction {1} -Expirable {2} -NotificationsEnabled {3} -Secondaries {4} -TimeToLive {5}";
        
        public NewCache()
        {
            InitializeComponent();
        }

        private void btnCreateNewCache_Click(object sender, EventArgs e)
        {
            var newCacheName = tbNewCacheName.Text;
            eviction = cmbEviction.SelectedValue == null ? eviction : cmbEviction.SelectedValue.ToString();
            ttl = string.IsNullOrEmpty(tbTTL.Text) ? ttl : Convert.ToInt32(tbTTL.Text);
            secondaries = cmbSecondaries.SelectedValue == null ? secondaries : (cmbSecondaries.SelectedValue.ToString().Equals("true", StringComparison.InvariantCultureIgnoreCase) ? 1 : 0);
            expirable = cmbExpirable.SelectedValue == null ? expirable : Convert.ToBoolean(cmbExpirable.SelectedValue);
            notificationEnabled = cmbNotificationEnabled.SelectedValue == null ? notificationEnabled: Convert.ToBoolean(cmbNotificationEnabled.SelectedValue);

            var executedMessage = string.Format("NamedCache {0} created successfully",newCacheName);
            var cmd=string.Format(NewCacheCmd, newCacheName,eviction,expirable,notificationEnabled,secondaries,ttl);
            bool isSuccess;
            ScriptHelper.RunScript(cmd, executedMessage, null, out isSuccess);
            if (isSuccess)
            {
                MessageBox.Show(executedMessage);
            }
        }

    }
}
